package com.sanjiahantang.youjiaclient.module.me.favorite.presenters;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.module.me.favorite.views.FavoriteView;
import com.sanjiahantang.youjiaclient.module.me.favorite.views.impl.FavoriteActivity;
import com.sanjiahantang.youjiaclient.module.me.favorite.views.impl.FavoriteCaseFragment;
import com.sanjiahantang.youjiaclient.module.me.favorite.views.impl.FavoriteGoodsFragment;


/**
 * Created by zy on 2017/9/5.
 */

public class FavoritePresenter extends BaseMvpPresenter<FavoriteView> {
    private static FragmentManager fMgr;
    public void initFragment(String tag) {
        FragmentTransaction ft = getMyFragmentManager().beginTransaction();
        FavoriteGoodsFragment goodsFragment =new FavoriteGoodsFragment();
        ft.add(R.id.mainContents, goodsFragment, tag);
        ft.addToBackStack(tag);
        ft.commitAllowingStateLoss();
    }

    private FragmentManager getMyFragmentManager(){
        if(fMgr == null){
            fMgr = mView.getMyFragmentManager();
        }
        return fMgr;
    }

    public void changeFragment(String tag) {
        FragmentTransaction ft = getMyFragmentManager().beginTransaction();
        if (fMgr.findFragmentByTag(tag) != null ) {
            ft.replace(R.id.mainContents,
                    fMgr.findFragmentByTag(tag),
                    tag);
            ft.commitAllowingStateLoss();
        } else {
            if(tag.equals(FavoriteView.TAG_GOODS)){
                FavoriteGoodsFragment goodsFragment = new FavoriteGoodsFragment();
                ft.replace(R.id.mainContents, goodsFragment,
                        tag);
                ft.addToBackStack(tag);
                ft.commit();
            }else {
                FavoriteCaseFragment caseFragment = new FavoriteCaseFragment();
                ft.replace(R.id.mainContents, caseFragment,
                        tag);
                ft.addToBackStack(tag);
                ft.commit();
            }

        }
    }

    @Override
    public void detach() {
        super.detach();
        fMgr = null;
    }

}
